<?php
use yii\helpers\Url;

$web = Url::base();
$this->title = '机构设置';
?>

<script type="application/javascript">
    $(function ($) {
        var record =<?=$record?>;
        $("#parentCode").val(record.parentCode);
        $("#institutionName_Parent").val(record.parentName);
        $("#code").val(record.code);

        $("#institutionName").attr("disabled", true);
        $("#isEnabled").bootstrapSwitch('disabled', true);
        $("#ForOrder").iCheck('disable', true);
        $("#ForSend").iCheck('disable', true);

        $("#institutionName").val(record.name);
        $("#institutionAddress").val(record.address);
        if (record.isAutoOrder == 0) {
            $("#isAutoOrder").bootstrapSwitch('state', true);
        } else {
            $("#isAutoOrder").bootstrapSwitch('state', false);
        }
        if (record.isCashOrder == 1) {
            $("#isCashOrder").bootstrapSwitch('state', true);
        } else {
            $("#isCashOrder").bootstrapSwitch('state', false);
        }
        if (record.isAllowImportOrder == 1) {
            $("#isAllowImportOrder").bootstrapSwitch('state', true);
        } else {
            $("#isAllowImportOrder").bootstrapSwitch('state', false);
        }
        if (record.billModel == 1) {
            $("#ForOrder").iCheck('check');
            $("#ForSend").iCheck('uncheck');
        } else {
            $("#ForSend").iCheck('check');
            $("#ForOrder").iCheck('uncheck');
        }
        if (record.isEnabled == 1) {
            $("#isEnabled").bootstrapSwitch('state', true);
        } else {
            $("#isEnabled").bootstrapSwitch('state', false);
        }
        if(record.isCustomPrice==1){
            $('#isCustomPrice').bootstrapSwitch('state', true);
        }else{
            $('#isCustomPrice').bootstrapSwitch('state', false);
        }
        $("#integralRate").attr("disabled", true);
        $("#integralUnit").attr("disabled", true);
        $("#integralRate").val(record.integralRate);
        $("#integralUnit").val(record.integralUnit);
        $("#remark").val(record.remark);
        $("#siteName").val(record.siteName);
        $("#domain").val(record.domain);
        if (record.domain == "") {
            $("#domain").hide();
            $("#domain_remark").show();
        }else{
            $("#domain").show();
        }
        $("#salesmanName").val(record.salesmanName);
        $("#salesmanPhone").val(record.salesmanPhone);

        //pc端模板加载
        var templatePc = record.themePc;
        var template_pc =<?= $template_pc?>;
        $.each(template_pc, function (i, item) {
            var text;
            if ((i == 0 & templatePc == null) || templatePc == item.img_url) {
                text = '<div class="pc item active"> <img src=' + item.img_url + ' class="img-responsive" alt="Responsive image"> </div>';

            } else {
                text = '<div class="pc item"> <img src=' + item.img_url + ' class="img-responsive" alt="Responsive image"> </div>';
            }
            $("#themePc").append(text);
        });

        //触屏端模板加载
        var templateM = record.themeM;
        var template_m =<?= $template_m?>;
        $.each(template_m, function (i, item) {
            var text;
            if ((i == 0 & themeM == null) || templateM == item.img_url) {
                text = '<div class="m item active"> <img src=' + item.img_url + ' class="img-responsive" alt="Responsive image"> </div>';

            } else {
                text = '<div class="m item"> <img src=' + item.img_url + ' class="img-responsive" alt="Responsive image"> </div>';
            }
            $("#themeM").append(text);
        });

        $("#template_m").val(templateM);

    });

    function addConfirm() {
        var data = {};
        data.institutionCode = $("#code").val();
        data.parentCode = $("#parentCode").val();
        data.name = $("#institutionName").val();
        data.address = $("#institutionAddress").val();

        if ($("#isEnabled").is(':checked') == true) {
            data.isEnabled = 1;
        } else {
            data.isEnabled = 0;
        }
        if ($("#isAutoOrder").is(':checked') == true) {
            data.isAutoOrder = 0;
        } else {
            data.isAutoOrder = 1;
        }
        if ($("#isCashOrder").is(':checked') == true) {
            data.isCashOrder = 1;
        } else {
            data.isCashOrder = 0;
        }
        if ($("#isAllowImportOrder").is(':checked') == true) {
            data.isAllowImportOrder = 1;
        } else {
            data.isAllowImportOrder = 0;
        }
        if ($("#ForOrder").is(':checked') == true) {
            data.billModel = 1;
        } else {
            data.billModel = 2;
        }
        if($('#isCustomPrice').is(':checked') == true){
            data.isCustomPrice=1;
        }else{
            data.isCustomPrice=0;
        }
        data.integralRate = $("#integralRate").val();
        data.integralUnit = $("#integralUnit").val();
        data.remark = $("#remark").val();
        data.siteName = $("#siteName").val();
        data.domain = $("#domain").val();
        data.themePc = $("div.pc.active img").attr("src");
//        data.themeM = $("div.m.active img").attr("src");
        data.themeM = $("#template_m").val();
        //data.account = $("#account").val();
        //data.token = $("#token").val();
        data.salesmanName = $("#salesmanName").val();
        data.salesmanPhone = $("#salesmanPhone").val();
        requestJson("<?=Url::to(['institution/add-institution-handler'])?>", data, true, function (re) {
            if (re.message == 0) {

                alert('操作成功！');
            } else {
                alert('操作失败！');
            }
            //$("#institutionModal3").modal('show');
        });

    }

</script>

<div class="panel panel-default">

    <div class="panel-body">
        <form class="form-horizontal adminex-form">
            <div class="form-group">
                <label class="col-lg-2 col-sm-3 control-label">机构名称</label>
                <div class="col-lg-10">
                    <input type="text" class="form-control" id="institutionName"">
                    <input class="hidden_id" id="code" type="hidden" value="0"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 col-sm-3 control-label">上级机构</label>
                <div class="col-lg-10">
                    <input type="text" class="form-control" id="institutionName_Parent" placeholder="无"
                           disabled="true">
                    <input class="hidden_id" id="parentCode" type="hidden" value="0"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 col-sm-3 control-label">地址</label>
                <div class="col-lg-10">
                    <input type="text" class="form-control" id="institutionAddress">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2">
                    <label class="checkbox-inline">
                        <div class="switch">
                            <input type="checkbox" id="isAutoOrder"><span
                                class="btn-clipboard popovers" data-trigger="hover"
                                data-placement="right"
                                data-content="开启后，会员兑换的订单需机构管理员在后台进行确认后，才会生效。"
                                data-original-title="提示">  订单需要审核</span>
                        </div>
                    </label>
                    <label class="checkbox-inline" style="display: none;">
                        <div class="switch">
                            <input type="checkbox" id="isCashOrder"><span
                                class="btn-clipboard popovers" data-trigger="hover"
                                data-placement="right"
                                data-content="开启后，允许会员使用现金（现金+积分）混合支付，兑换产品。否则禁止用户使用现金。"
                                data-original-title="提示">  支持现金支付</span>
                        </div>
                    </label>
                    <label class="checkbox-inline">
                        <div class="switch">
                            <input type="checkbox" id="isAllowImportOrder"><span
                                class="btn-clipboard popovers" data-trigger="hover"
                                data-placement="right"
                                data-content="开启后，允许机构管理员从后台批量导入兑换订单。"
                                data-original-title="提示">  允许导入订单</span>
                        </div>
                    </label>
                    <label class="checkbox-inline">
                        <div class="switch">
                            <input id="isEnabled" type="checkbox" checked/>
                            <span>启用  </span>
                        </div>

                    </label>
                    <?php
                    $isAdmin = \app\models\Permission::isSystemAdmin();
                    $style = '';
                    if (!$isAdmin) {
                        $style = 'style="display: none"';
                    }
                    ?>
                    <label class="checkbox-inline" <?= $style ?>>
                        <div class="switch">
                            <input type="checkbox" id="isCustomPrice"><span
                                class="btn-clipboard popovers" data-trigger="hover"
                                data-placement="right"
                                data-content="开启后，允许机构自主定价。"
                                data-original-title="提示">  允许自主定价</span>
                        </div>
                    </label>
                </div>
            </div>

            <div class="form-group" <?= $style ?>>
                <label class="col-lg-2 col-sm-3 control-label">结算方式</label>
                <div class="col-lg-10">
                    <label class="control-label">
                        <div class="square-blue">
                            <div class="radio">
                                <div class="square-blue form-inline icheck">
                                    <div class="radio popovers">
                                        <input type="radio" name="optionsRadios" id="ForOrder" value="1">
                                        按订单结算
                                    </div>
                                </div>
                            </div>
                        </div>
                    </label>
                    <label class="control-label">
                        <div class="square-blue">
                            <div class="radio">
                                <div class="square-blue form-inline icheck">
                                    <div class="radio popovers">
                                        <input type="radio" name="optionsRadios" id="ForSend" value="2">
                                        按发货结算
                                    </div>
                                </div>
                            </div>
                        </div>

                    </label>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 col-sm-3 control-label">汇率</label>
                <label class="col-lg-2 control-label">1元=</label>
                <div class="col-sm-2">
                    <input type="number" class="form-control" id="integralRate" value="1">
                </div>
                <div class="col-sm-2">
                    <input type="text" class="form-control" id="integralUnit" value="积分">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 col-sm-3 control-label">备注</label>
                <div class="col-lg-10">
                    <textarea rows="6" class="form-control" id="remark"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 col-sm-3 control-label">网站名称</label>
                <div class="col-lg-10">
                    <input type="text" class="form-control" id="siteName">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 col-sm-3 control-label">域名</label>
                <div class="col-lg-10">
                    <input type="text" class="form-control" id="domain" <?= $style ?>>
                    <input type="text" class="form-control" id="domain_remark" style="display: none"
                           value="如需域名，请联系朵拉客服，联系电话400-860-3111">
                </div>
            </div>
            <div class="form-group" style="display: none;">
                <label class="col-lg-2 col-sm-3 control-label">PC模板</label>
                <div class="col-sm-5">
                    <div id="carousel-example-pc" class="carousel" data-interval="false">
                        <!-- Wrapper for slides -->
                        <div class="carousel-inner" role="listbox" id="themePc">


                        </div>

                        <!-- Controls -->
                        <a class="left carousel-control" href="#carousel-example-pc" role="button"
                           data-slide="prev">
                            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                            <span class="sr-only">Previous</span>
                        </a>
                        <a class="right carousel-control" href="#carousel-example-pc" role="button"
                           data-slide="next">
                            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                            <span class="sr-only">Next</span>
                        </a>
                    </div>
                </div>
            </div>
            <div class="form-group" style="display: none;">
                <label class="col-lg-2 col-sm-3 control-label">触屏模板</label>
                <input type="hidden" id="template_m">
                <div class="col-sm-5">
                    <div id="carousel-example-m" class="carousel" data-interval="false">
                        <!-- Wrapper for slides -->
                        <div class="carousel-inner" role="listbox" id="themeM">

                        </div>

                        <!-- Controls -->
                        <a class="left carousel-control" href="#carousel-example-m" role="button"
                           data-slide="prev">
                            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                            <span class="sr-only">Previous</span>
                        </a>
                        <a class="right carousel-control" href="#carousel-example-m" role="button"
                           data-slide="next">
                            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                            <span class="sr-only">Next</span>
                        </a>
                    </div>
                </div>
            </div>
            <div class="form-group" style="display: none">
                <label class="col-lg-2 col-sm-3 control-label">接入账号</label>
                <div class="col-lg-10">
                    <input type="text" class="form-control" id="account" placeholder="前端站点通过api接口接入系统的账号">
                </div>
            </div>
            <div class="form-group" style="display: none">
                <label class="col-lg-2 col-sm-3 control-label">TOKEN</label>
                <div class="col-lg-10">
                    <input type="text" class="form-control" id="token" placeholder="前端站点通过api接口接入系统的token">
                </div>
            </div>
            <div class="form-group" style="display: none">
                <label class="col-lg-2 col-sm-3 control-label">销售人员</label>
                <div class="col-sm-2">
                    <input type="text" class="form-control" id="salesmanName" placeholder="姓名">
                </div>
                <div class="col-sm-2">
                    <input type="text" class="form-control" id="salesmanPhone" placeholder="电话">
                </div>
            </div>
        </form>
    </div>
    <div class="panel-footer">
        <button class="btn btn-primary" onclick="addConfirm()">保存</button>
    </div>
</div>

<div aria-hidden="true" role="dialog" tabindex="-1" id="institutionModal3" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">消息</h4>
            </div>
            <div class="modal-body" id="message">

                操作成功！

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal"> Ok</button>
            </div>
        </div>
    </div>
</div>